package assetservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type AssetBasicInfoPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetBasicInfoPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetBasicInfoPageLogic {
	return &AssetBasicInfoPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetBasicInfoPage 分页查询资产信息表项
func (l *AssetBasicInfoPageLogic) AssetBasicInfoPage(in *property_service.AssetBasicInfoPageReq) (*property_service.AssetBasicInfoPageRes, error) {
	// 定义一个资产信息表模型对象切片用于存储查询结果
	var data []*model.AssetBasicInfo
	// 获取资产信息表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetBasicInfo
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID))
	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		if in.Where.AssetIds != nil {
			do = do.Where(m.ID.In(in.Where.AssetIds...)) // ID列表
		}
		if in.LastDate != "" {
			//如果查询折旧期间不为空，则需要过滤当前时间段已经折旧过的资产信息
			var data1 []*model.AssetDepreciation
			// 获取资产折旧信息表模型的查询构造器
			m1 := query.Use(l.svcCtx.Gorm).AssetDepreciation
			// 构建查询条件：根据租户ID查询
			do1 := m1.WithContext(l.ctx).Where(m1.TenantID.Eq(in.Where.TenantID))
			// 获取本月已折旧的数据
			do1 = do1.Where(m1.DepreciationPeriod.Eq(in.LastDate))
			// 执行查询
			// 如果不需要分页，查询所有记录
			data1, _ = do1.Find()
			//获取查询到的，如果没有则直接返回空
			if len(data1) > 0 {
				//如果已经有生成的，查询已经生成的资产ID
				var assetIds []int64
				//循环将已生成的资产信息添加到assetIds表中
				for _, v := range data1 {
					assetIds = append(assetIds, v.AssetID)
				}
				//查询不在已生成折旧的资产信息
				do = do.Where(m.ID.NotIn(assetIds...))
			}
			//否则查询所有
		}

		// 处理字符类型字段，进行模糊查询
		if in.Where.AssetCode != "" {
			do = do.Where(m.AssetCode.Like("%" + in.Where.AssetCode + "%")) // 资产编码
		}
		if in.Where.AssetName != "" {
			do = do.Where(m.AssetName.Like("%" + in.Where.AssetName + "%")) // 资产名称
		}
		if in.Where.BindingCode != "" {
			do = do.Where(m.BindingCode.Like("%" + in.Where.BindingCode + "%")) // 科目分类编码
		}
		if in.Where.BindingName != "" {
			do = do.Where(m.BindingName.Like("%" + in.Where.BindingName + "%")) // 科目分类名称
		}
		if in.Where.AssetStatus != "" {
			do = do.Where(m.AssetStatus.Like("%" + in.Where.AssetStatus + "%")) // 资产状态
		}
		if in.Where.AssetCategory != "" {
			do = do.Where(m.AssetCategory.Like("%" + in.Where.AssetCategory + "%")) // 资产分类
		}
		if in.Where.AssetSerialNo != "" {
			do = do.Where(m.AssetSerialNo.Like("%" + in.Where.AssetSerialNo + "%")) // 资产序列号
		}
		if in.Where.CountryOfOrigin != "" {
			do = do.Where(m.CountryOfOrigin.Like("%" + in.Where.CountryOfOrigin + "%")) // 产地
		}
		if in.Where.StorageLocation != "" {
			do = do.Where(m.StorageLocation.Like("%" + in.Where.StorageLocation + "%")) // 存放地点
		}
		if in.Where.AssetSource != "" {
			do = do.Where(m.AssetSource.Like("%" + in.Where.AssetSource + "%")) // 资产来源
		}
		if in.Where.RegistrationNo != "" {
			do = do.Where(m.RegistrationNo.Like("%" + in.Where.RegistrationNo + "%")) // 注册证书编号
		}
		if in.Where.DocumentNo != "" {
			do = do.Where(m.DocumentNo.Like("%" + in.Where.DocumentNo + "%")) // 档案编号
		}
		if in.Where.OriginalAssetCode != "" {
			do = do.Where(m.OriginalAssetCode.Like("%" + in.Where.OriginalAssetCode + "%")) // 原资产编码
		}
		if in.Where.AssetStatusFlag != "" {
			do = do.Where(m.AssetStatusFlag.Like("%" + in.Where.AssetStatusFlag + "%")) // 资产状态标识
		}
		if in.Where.UsageRoom != "" {
			do = do.Where(m.UsageRoom.Like("%" + in.Where.UsageRoom + "%")) // 使用科室
		}
		if in.Where.AssetOwner != "" {
			do = do.Where(m.AssetOwner.Like("%" + in.Where.AssetOwner + "%")) // 资产负责人
		}
		if in.Where.ManufacturerCode != "" {
			do = do.Where(m.ManufacturerCode.Like("%" + in.Where.ManufacturerCode + "%")) // 生产厂家编码
		}
		if in.Where.PositionStandard != "" {
			do = do.Where(m.PositionStandard.Like("%" + in.Where.PositionStandard + "%")) // 定位标准
		}
		if in.Where.ElectronicTag != "" {
			do = do.Where(m.ElectronicTag.Like("%" + in.Where.ElectronicTag + "%")) // 电子标签
		}
		if in.Where.AssetType != "" {
			do = do.Where(m.AssetType.Like("%" + in.Where.AssetType + "%")) // 资产类型
		}
		if in.Where.AssetModel != "" {
			do = do.Where(m.AssetModel.Like("%" + in.Where.AssetModel + "%")) // 规格型号
		}
		if in.Where.Brand != "" {
			do = do.Where(m.Brand.Like("%" + in.Where.Brand + "%")) // 品牌
		}
		if in.Where.WarrantyCertificateNo != "" {
			do = do.Where(m.WarrantyCertificateNo.Like("%" + in.Where.WarrantyCertificateNo + "%")) // 保修凭证编号
		}
		if in.Where.InvoiceNo != "" {
			do = do.Where(m.InvoiceNo.Like("%" + in.Where.InvoiceNo + "%")) // 发票编号
		}
		if in.Where.ContractNo != "" {
			do = do.Where(m.ContractNo.Like("%" + in.Where.ContractNo + "%")) // 合同编号
		}
		if in.Where.SupplierName != "" {
			do = do.Where(m.SupplierName.Like("%" + in.Where.SupplierName + "%")) // 供应商
		}
		if in.Where.ManufacturersName != "" {
			do = do.Where(m.ManufacturersName.Like("%" + in.Where.ManufacturersName + "%")) // 生产商
		}
		if in.Where.MaintenanceName != "" {
			do = do.Where(m.MaintenanceName.Like("%" + in.Where.MaintenanceName + "%")) // 维保商
		}
		if in.Where.PurchaseOrderName != "" {
			do = do.Where(m.PurchaseOrderName.Like("%" + in.Where.PurchaseOrderName + "%")) // 采购订单
		}
		if in.Where.StatusName != "" {
			do = do.Where(m.StatusName.Like("%" + in.Where.StatusName + "%")) // 状态名称
		}
		if in.Where.ManageRoom != "" {
			do = do.Where(m.ManageRoom.Like("%" + in.Where.ManageRoom + "%")) // 状态名称
		}
		if in.Where.WaysOfChanging != "" {
			do = do.Where(m.WaysOfChanging.Like("%" + in.Where.WaysOfChanging + "%")) // 状态名称
		}
		if in.Where.Unit != "" {
			do = do.Where(m.Unit.Like("%" + in.Where.Unit + "%")) // 状态名称
		}
		if in.Where.ManyDepartmentID != "" {
			do = do.Where(m.ManyDepartmentID.Like("%" + in.Where.ManyDepartmentID + "%")) // 状态名称
		}
		if in.Where.ManyDepartmentName != "" {
			do = do.Where(m.ManyDepartmentName.Like("%" + in.Where.ManyDepartmentName + "%")) // 状态名称
		}
		if in.Where.ManyDepartmentRate != "" {
			do = do.Where(m.ManyDepartmentRate.Like("%" + in.Where.ManyDepartmentRate + "%")) // 状态名称
		}
		if in.Where.InventoryCode != "" {
			do = do.Where(m.InventoryCode.Like("%" + in.Where.InventoryCode + "%")) // 入库单编码
		}
		if in.Where.PurchaseOrderCode != "" {
			do = do.Where(m.PurchaseOrderCode.Like("%" + in.Where.PurchaseOrderCode + "%")) // 采购订单编码
		}
		if in.Where.FiscalAssetCode != "" {
			do = do.Where(m.FiscalAssetCode.Like("%" + in.Where.FiscalAssetCode + "%")) // 财政资产编码(审核的时候填写)
		}
		if in.Where.FiscalAssetName != "" {
			do = do.Where(m.FiscalAssetName.Like("%" + in.Where.FiscalAssetName + "%")) // 财政资产名称
		}

		// 处理时间类型字段，进行精确查询
		if in.Where.WarrantyPeriod != "" {
			do = do.Where(m.WarrantyPeriod.Eq(BeanUtils.GetDateFormat(in.Where.WarrantyPeriod))) // 保修到期日
		}
		if in.Where.PurchaseDate != "" {
			do = do.Where(m.PurchaseDate.Eq(BeanUtils.GetDateFormat(in.Where.PurchaseDate))) // 购买日期
		}
		if in.Where.ImportDate != "" {
			do = do.Where(m.ImportDate.Eq(BeanUtils.GetDateFormat(in.Where.ImportDate))) // 进口日期
		}
		if in.Where.UseDate != "" {
			do = do.Where(m.UseDate.Eq(BeanUtils.GetDateFormat(in.Where.UseDate))) // 进口日期
		}

		// 处理数字类型字段，进行精确查询
		if in.Where.AssetCategoryID > 0 {
			do = do.Where(m.AssetCategoryID.Eq(in.Where.AssetCategoryID)) // 资产分类ID
		}
		if in.Where.CountryOfOriginID > 0 {
			do = do.Where(m.CountryOfOriginID.Eq(in.Where.CountryOfOriginID)) // 产地ID
		}
		if in.Where.StorageLocationID > 0 {
			do = do.Where(m.StorageLocationID.Eq(in.Where.StorageLocationID)) // 存放地点ID
		}
		if in.Where.AssetSourceID > 0 {
			do = do.Where(m.AssetSourceID.Eq(in.Where.AssetSourceID)) // 资产来源ID
		}
		if in.Where.UsageRoomID > 0 {
			do = do.Where(m.UsageRoomID.Eq(in.Where.UsageRoomID)) // 使用科室ID
		}
		if in.Where.AssetOwnerID > 0 {
			do = do.Where(m.AssetOwnerID.Eq(in.Where.AssetOwnerID)) // 资产负责人ID
		}
		if in.Where.ManufacturerCodeID > 0 {
			do = do.Where(m.ManufacturerCodeID.Eq(in.Where.ManufacturerCodeID)) // 生产厂家编码ID
		}
		if in.Where.AssetTypeID > 0 {
			do = do.Where(m.AssetTypeID.Eq(in.Where.AssetTypeID)) // 资产类型ID
		}
		if in.Where.BrandID > 0 {
			do = do.Where(m.BrandID.Eq(in.Where.BrandID)) // 品牌ID
		}
		if in.Where.SupplierID > 0 {
			do = do.Where(m.SupplierID.Eq(in.Where.SupplierID)) // 供应商ID
		}
		if in.Where.ManufacturersID > 0 {
			do = do.Where(m.ManufacturersID.Eq(in.Where.ManufacturersID)) // 生产商ID
		}
		if in.Where.SlMj > 0 {
			do = do.Where(m.SlMj.Eq(in.Where.SlMj)) // 数量面积
		}
		if in.Where.MaintenanceID > 0 {
			do = do.Where(m.MaintenanceID.Eq(in.Where.MaintenanceID)) // 维保商ID
		}
		if in.Where.PurchaseOrderID > 0 {
			do = do.Where(m.PurchaseOrderID.Eq(in.Where.PurchaseOrderID)) // 采购订单ID
		}
		if in.Where.StatusID > 0 {
			do = do.Where(m.StatusID.Eq(in.Where.StatusID)) // 状态ID
		}
		if in.Where.StockID > 0 {
			do = do.Where(m.StockID.Eq(in.Where.StockID)) // 仓库ID
		}
		if in.Where.ManageRoomID > 0 {
			do = do.Where(m.ManageRoomID.Eq(in.Where.ManageRoomID)) // 仓库ID
		}
		if in.Where.WaysOfChangingID != "" {
			do = do.Where(m.WaysOfChangingID.Eq(in.Where.WaysOfChangingID)) // 仓库ID
		}
		if in.Where.IsManyDepartment > 0 {
			do = do.Where(m.IsManyDepartment.Eq(in.Where.IsManyDepartment)) // 仓库ID
		}
		if in.Where.InventoryID > 0 {
			do = do.Where(m.InventoryID.Eq(in.Where.InventoryID)) // 入库单ID
		}
		if in.Where.IsStock > 0 {
			do = do.Where(m.IsStock.Eq(in.Where.IsStock)) // 是否在库(0 在库/1 出库)
		}
	}

	// 定义一个变量用于存储总记录数
	var count int64

	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		do.Order(m.CreatedAt)
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		do.Order(m.CreatedAt.Desc())
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.AssetBasicInfo{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.AssetBasicInfoPageRes{
		Count: count,
		List:  list.([]*property_service.AssetBasicInfo),
	}, nil
}
